
import itertools
import copy
# 字牌 带顺

# 分 风牌 和 箭牌
# 再合起来

base_feng_tb = [
    [0,0,0,0],
    [0,1,1,1],
    [1,0,1,1],
    [1,1,0,1],
    [1,1,1,0],
    [3,0,0,0],
    [0,3,0,0],
    [0,0,3,0],
    [0,0,0,3]
]

base_jian_tb = [
    [0,0,0],
    [1,1,1],
    [3,0,0],
    [0,3,0],
    [0,0,3]
]

ttt = {}
zi_with_shun_table = {}
eye_zi_with_shun_table = {}


def perm(list1):
    list2 = []
    for i in range(0,len(list1)):
        iter = itertools.combinations_with_replacement(list1,i)
        for j in iter:
            if list(j) not in list2:
                list2.append(list(j))
    return list2

print(perm(range(0,9)))

def get_feng_combo_list():
    feng_list = perm(range(0, 9))
    feng_combo_list = []
    feng_use_three = {} #[key] -> 0

    for feng in feng_list:
        tmp_feng_list = [0, 0, 0, 0]
        tmp_feng = copy.deepcopy(feng)
        tIndex = 0
        while tIndex != len(tmp_feng):
            if tmp_feng[tIndex] == 0:
                del tmp_feng[tIndex]
            else:
                tIndex += 1

        use_len = len(tmp_feng)
        if use_len > 4:
            continue
        flag = True
        for data in feng:
            for i in range(0, 4):
                tmp_feng_list[i] += base_feng_tb[data][i]
                if tmp_feng_list[i] > 4:
                    flag = False
                    break
        if flag:
            copy_tmp_feng_list = copy.deepcopy(tmp_feng_list)
            for index, data in enumerate(copy_tmp_feng_list):
                copy_tmp_feng_list[index] = str(data)
            key_str = ''.join(copy_tmp_feng_list)
            if tmp_feng_list not in feng_combo_list:
                feng_use_three.setdefault(key_str, [])
                feng_combo_list.append(tmp_feng_list)
            if use_len not in feng_use_three[key_str]:
                feng_use_three[key_str].append(use_len)

    return feng_combo_list,feng_use_three

def get_jian_combo_list():
    jian_list = perm(range(0, 5))
    jian_use_three = {} # 0 -> [key]

    for jian in jian_list:
        tmp_jian_list = [0, 0, 0]

        tmp_jian = copy.deepcopy(jian)
        tIndex = 0
        while tIndex != len(tmp_jian):
            if tmp_jian[tIndex] == 0:
                del tmp_jian[tIndex]
            else:
                tIndex += 1

        use_len = len(tmp_jian)
        if use_len > 4:
            continue
        flag = True
        for data in jian:
            for i in range(0, 3):
                tmp_jian_list[i] += base_jian_tb[data][i]
                if tmp_jian_list[i] > 4:
                    flag = False
                    break
        if flag:
            if use_len not in jian_use_three:
                jian_use_three.setdefault(use_len, [])

            if tmp_jian_list not in jian_use_three[use_len]:
                jian_use_three[use_len].append(tmp_jian_list)

    return jian_use_three


def get_base_table():
    feng_combo_list,feng_use_three = get_feng_combo_list()

    jian_use_three = get_jian_combo_list()

    copy_feng_combo_list = copy.deepcopy(feng_combo_list)
    for index, data in enumerate(copy_feng_combo_list):
        for i,d in enumerate(data):
            copy_feng_combo_list[index][i] = str(d)

    zi_combo_list = []
    for index,data in enumerate(copy_feng_combo_list):
        key_str = ''.join(data)
        left_list = feng_use_three[key_str]
        for left in left_list:
            left_num = 4 - left
            for num in range(0,left_num + 1):
                for jian_list in jian_use_three[num]:
                    tmp_zi_combo_list = copy.deepcopy(feng_combo_list[index])
                    tmp_zi_combo_list.extend(jian_list)
                    zi_combo_list.append(tmp_zi_combo_list)
    return zi_combo_list

def gen_eye_base_table():

    zi_combo_list = get_base_table()
    zi_eye_combo_list = []
    value_list = []

    for zi_list in zi_combo_list:
        for i in range(0, 7):
            tmp_eye_combo_list = copy.deepcopy(zi_list)
            if zi_list[i] > 2:
                continue
            tmp_eye_combo_list[i] += 2
            value_list.append(1 + ((i + 1) << 4) + (0 << 8))
            zi_eye_combo_list.append(tmp_eye_combo_list)

    return zi_eye_combo_list,value_list

def add(key,value):
    if key % 3 == 0:
        if key not in zi_with_shun_table:
            zi_with_shun_table.setdefault(key, [])
        zi_with_shun_table[key] = 0
    else:
        if key not in eye_zi_with_shun_table:
            eye_zi_with_shun_table.setdefault(key, [])
        eye_zi_with_shun_table[key] = value


def add_table(e_list,value = 0):
    key_num = 0
    for i in range(0,7):
        key_num = key_num * 10 + e_list[i]

    if key_num in ttt:
        return

    ttt.setdefault(key_num, True)

    add(key_num,value)

# zi_combo_list = get_base_table()
# zi_eye_combo_list,value_list = gen_eye_base_table()
# for combo_list in zi_combo_list:
#     add_table(combo_list)
#
# for index,data in enumerate(zi_eye_combo_list):
#     add_table(data,value_list[index])
#
# with open("E:\PythonWorkSpace\generate_list_hu\\tables\\zi_with_shun_table.txt","w") as file:
#     for s in zi_with_shun_table:
#         # ss = "zi_with_shun_table.insert(std::pair<uint32_t,bool>({key},true));".format(key = s)
#         ss = "{key},{value}".format(key=s,value=zi_with_shun_table[s])
#         file.writelines(ss + "\n")
#
# with open("E:\PythonWorkSpace\generate_list_hu\\tables\eye_zi_with_shun_table.txt","w") as file:
#     for s in eye_zi_with_shun_table:
#         # ss = "eye_zi_with_shun_table.insert(std::pair<uint32_t,bool>({key},true));".format(key = s)
#         ss = "{key},{value}".format(key=s,value=eye_zi_with_shun_table[s])
#         file.writelines(ss + "\n")
